$(function() {
    var Order_col='default',
        order_type='desc';
        
    LoadData(1,true,'default','desc');  
    
    function LoadData(page,b, order, typeOrder){
        
      $( "#resultData tbody tr" ).remove();
      
       showLoader();
       
       var dataSet={'m':'getlist'
                        ,'order':order
                        ,'typeOrder':typeOrder
                        };      
                        
        $.ajax({
                type: 'POST',
                cache: false,
                url: 'model.php',
                data: dataSet,      
                success: function(data){
                   
                  var obj=eval("(" + data + ")");
                
                  hideLoader();
                   $( "#resultData tbody tr" ).remove();
                   $( "#resultData tbody" ).append(obj['data']);
                     $(".totalGB").html( parseFloat(obj['totalGB']).toFixed(2) );   
            }, 
            error:function(jqXHR, textStatus, errorThrown) { 
                alert(jqXHR+":"+textStatus);
              }
            });
                                          
           
     }
 
    $(".btnDelete").live('click',function(){
            var answer = confirm("ยืนยัน การลบรายการ?");
            var rowSelect = $(this).parent().parent();
            var mid  =rowSelect.find("td").eq(1).html();
            
            var dataSet={'m':'dellist','order':Order_col,'i':mid,'typeOrder':order_type};
                              
            if (answer){
                  $.ajax({
                    type: 'POST',
                    cache: false,
                    url: 'model.php',
                    data: dataSet,      
                    success: function(data){
                       
                  var obj=eval("(" + data + ")");
                
                  hideLoader();
                   $( "#resultData tbody tr" ).remove();
                   $( "#resultData tbody" ).append(obj['data']);
                    $(".totalGB").html( parseFloat(obj['totalGB']).toFixed(2) );    
                }, 
                error:function(jqXHR, textStatus, errorThrown) { 
                    alert(jqXHR+":"+textStatus);
                  }
                }); 
                        
                
            }
           
    });
    
    $("#btnOrder").click(function(){
            var bValid = true;
           // allFields.removeClass( "ui-state-error" );
            bValid = bValid && checkInputFile( $("#txtReName"), "ชื่อผู้รับ",1 );
            bValid = bValid && checkInputFile( $("#txtAddress"), "ที่อยู่ในการจัดส่ง",1 );
            
            if (bValid){
                var name =$("#txtReName").val();
                var address = $("#txtAddress").val();
                var hdd = $("#cboHdd").val();
               
                var size=$(".totalGB").html();
                var dataSet={'m':'order','name':name,'addr':address,'hdd':hdd,'s':size};
                $.ajax({
                type: 'POST',
                cache: false,
                url: 'model.php',
                data: dataSet,      
                success: function(data){
                 
                  var obj=eval("(" + data + ")");
                  if (obj['msg']==1)
                        alert('บันทึกคำสั่งซื้อเรียบร้อยแล้ว');
                        
                   window.location="payment.php";     
                }, 
                error:function(jqXHR, textStatus, errorThrown) { 
                    alert(jqXHR+":"+textStatus);
                  }
                });
                       
                
            }
    });
    
    
    $("a.sort").click(function(){
        var id = $(this).attr('id');
        order_type =$("#ord").val();
        //  alert(type);
        Order_col=id;
        LoadData (1,true, Order_col, order_type);
        
        if (order_type=='desc')
            $("#ord").val('asc');
        else{
            $("#ord").val('desc');
        }
 
    });
    
    function updateTips( t ) {
         $(".message").css("display" ,'block');
         $(".message").find("p").html(t); 
        setTimeout(function() {
           $(".message").css("display" ,'none');
        }, 5000 );
    }
    
    function checkInputFile( o, n, min ) {
        if (o.val().length < min ) {
            o.addClass( "ui-state-error" );
            updateTips( "กรุณาระบุบ " + n + "." );
            return false;
        } else {
            return true;
        }
    }
    //Show Loading bar          
    function showLoader(){
        $( "#resultData tbody:first" ).prepend( "<tr>"
                    +"<td colspan='7' class='search-background' align='center'>"
                    +"<label><img src='images/ajax-loader.gif'/></label>"
                    +"</td>"
                    +"</tr>" ); 
         $('.search-background').fadeIn(900,0);
    }
    //Hide Loading bar          
    function hideLoader(){
        $('.search-background').fadeOut(300);
        $( "#resultData tbody tr" ).remove();
    };
    
});